//
//  TKYSelectedPersonController.m
//  StationHouseHelper
//
//  Created by apple on 2018/7/3.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYSelectedPersonController.h"
#import "TKYSearchBarTextFiled.h"

#import "WMPageController.h"
#import "ParentClassScrollViewController.h"
#import "MainTouchTableTableView.h"

#define Main_Screen_Height      [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width       [[UIScreen mainScreen] bounds].size.width
@interface TKYSelectedPersonController()<UITableViewDelegate,UITableViewDataSource,scrollDelegate,WMPageControllerDelegate,UISearchBarDelegate>



@property(nonatomic ,strong)MainTouchTableTableView * mainTableView;

@property(nonatomic,strong) UIScrollView * parentScrollView;
@property (nonatomic, strong) UIView *headerView;

/**
 搜索框
 */
//@property (nonatomic, strong)TKYSearchBarTextFiled *searchBar;

@property(nonatomic,strong) UISearchBar *searchBar;// 搜索条




@end

@implementation TKYSelectedPersonController
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!_searchBar.isFirstResponder) {
        [self.searchBar becomeFirstResponder];
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.searchBar resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置界面
    [self setUPUI];
    
   // [self.selectedMemberVc.tableView reloadData];
    [self.view addSubview:self.mainTableView];
    //添加搜索
    [self setBarButtonItem];
    
    self.mainTableView.tableHeaderView = self.headerView;
    
}

- (void)setBarButtonItem
{
    //隐藏导航栏上的返回按钮
    [self.navigationItem setHidesBackButton:YES];
    //用来放searchBar的View
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, self.view.frame.size.width, 40)];
    //创建searchBar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 15, 40)];
    //默认提示文字
    searchBar.placeholder = @"搜索内容";
    //背景图片
    searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"];
    //代理
    searchBar.delegate = self;
    //显示右侧取消按钮
    searchBar.showsCancelButton = YES;
    //光标颜色
    searchBar.tintColor = [UIColor redColor];
    //拿到searchBar的输入框
    UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
    //字体大小
    searchTextField.font = [UIFont systemFontOfSize:15];
    //输入框背景颜色
    searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
    //拿到取消按钮
    UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"];
    //设置按钮上的文字
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    //设置按钮上文字的颜色
    [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [titleView addSubview:searchBar];
    self.searchBar = searchBar;
    //self.navigationItem.titleView = titleView;
    [self.headerView addSubview: self.searchBar];
}


#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
    NSString *inputStr = searchBar.text;
    //[self.results removeAllObjects];
    self.selectedMemberVc.results = [NSMutableArray array];
    for (NSString *str in self.selectedMemberVc.dataSourceArr) {
        if ([str.lowercaseString rangeOfString:inputStr.lowercaseString].location != NSNotFound) {
            [self.selectedMemberVc.results addObject:str];
        }
    }
    
    NSLog(@"%@",self.selectedMemberVc.results);
    [self.selectedMemberVc.tableView reloadData];
    NSLog(@"SearchButton");
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSString *inputStr = searchText;
    
    //成员
    [self.selectedMemberVc.results removeAllObjects];
    self.selectedMemberVc.results = [NSMutableArray array];
    for (NSString *str in self.selectedMemberVc.dataSourceArr) {
        if ([str.lowercaseString rangeOfString:inputStr.lowercaseString].location != NSNotFound) {
            [self.selectedMemberVc.results addObject:str];
        }
    }
    [self.selectedMemberVc.tableView reloadData];
    
//    //分组
//    [self.selectedGroupVc.results removeAllObjects];
//    self.selectedGroupVc.results = [NSMutableArray array];
//    NSMutableArray *sectionArr = [NSMutableArray array];
//
//    for (NSArray *arr in self.selectedGroupVc.dataSource) {
//        for (NSString *str in arr) {
//            if ([str.lowercaseString rangeOfString:inputStr.lowercaseString].location != NSNotFound) {
//                [sectionArr addObject:str];
//            }
//            [self.selectedGroupVc.results addObject:sectionArr];
//        }
//    }
//    [self.selectedGroupVc.tableV reloadData];
}

/**
 设置界面
 */
- (void)setUPUI{
    self.navigationItem.title = @"选择成员";
    self.navigationController.navigationBar.barTintColor = KNavColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
    
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(ture)];
}

- (void)back{
    NSLog(@"%@",self.selectedMemberVc.selectDataArr);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)ture{
    NSLog(@"%@", self.selectedMemberVc.selectDataArr);
    self.selectedMemberBlock(self.selectedMemberVc.selectDataArr);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma scrollDelegate
-(void)scrollViewLeaveAtTheTop:(UIScrollView *)scrollView
{
    self.parentScrollView = scrollView;
    //离开顶部 主View 可以滑动
    
    self.parentScrollView.scrollEnabled = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //获取滚动视图y值的偏移量
    CGFloat tabOffsetY = [self.mainTableView rectForSection:0].origin.y;
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY>=tabOffsetY) {
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        self.parentScrollView.scrollEnabled = YES;
    }else{
    }
    
}

#pragma mark --tableDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return Main_Screen_Height-64;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    /* 添加pageView
     * 这里可以任意替换你喜欢的pageView
     *作者这里使用一款github较多人使用的 WMPageController 地址https://github.com/wangmchn/WMPageController
     */
    [cell.contentView addSubview:self.setPageViewControllers];
    
    return cell;
}


-(UIView *)setPageViewControllers
{
    WMPageController *pageController = [self p_defaultController];
    pageController.title = @"Line";
    pageController.menuViewStyle = WMMenuViewStyleLine;
    pageController.titleSizeSelected = 15;
    
    [self addChildViewController:pageController];
    [pageController didMoveToParentViewController:self];
    return pageController.view;
}

- (WMPageController *)p_defaultController {
    //准备要添加的控制器和标题数组
    self.selectedMemberVc = [[TKYSelectedMemberController alloc]init];
    //self.selectedGroupVc = [[TKYSelectedGroupController alloc]init];
    self.selectedCompanyVc = [[TKYSelectedCompanyController alloc]init];
    self.selectedMemberVc.delegate = self;
    //self.selectedGroupVc.delegate = self;
    self.selectedCompanyVc.delegate = self;
    
    NSArray *viewControllers = @[self.selectedMemberVc,self.selectedCompanyVc];
    
    NSArray *titles = @[@"按成员显示",@"按公司显示"];
    WMPageController *pageVC = [[WMPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    [pageVC setViewFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];
    pageVC.delegate = self;
    pageVC.menuItemWidth = 85;
    pageVC.titleColorSelected = KNavColor;
    pageVC.menuHeight = 44;
    pageVC.postNotification = YES;
    pageVC.bounces = YES;
    return pageVC;
}

- (void)pageController:(WMPageController *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info {
    
    NSLog(@"%@",viewController);
}

-(MainTouchTableTableView *)mainTableView
{
    if (_mainTableView == nil)
    {
        _mainTableView= [[MainTouchTableTableView alloc]initWithFrame:CGRectMake(0,MyNavBarHeight,Main_Screen_Width,Main_Screen_Height- MyNavBarHeight)];
        _mainTableView.delegate=self;
        _mainTableView.dataSource=self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        // _mainTableView.contentInset = UIEdgeInsetsMake(headViewHeight,0, 0, 0);
        _mainTableView.backgroundColor = [UIColor clearColor];
    }
    return _mainTableView;
}

//- (TKYSearchBarTextFiled *)searchBar{
//    if (_searchBar == nil) {
//        _searchBar = [[TKYSearchBarTextFiled alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeightScale *40)];
//        _searchBar.seachbarBlock = ^(UIButton *button) {
//            NSLog(@"点击了搜索按钮");
//        };
//    }
//    return _searchBar;
//}

- (UIView *)headerView{
    if (_headerView == nil) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeightScale *45)];
        _headerView.backgroundColor = [UIColor redColor];
    }
    return _headerView;
}




@end
